4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
22 use Composer\Spdx\SpdxLicenses
;
23 use JsonSchema\Validator
;
28 class ExtensionJsonValidator
{
33 private $missingDepCallback;
36 * @param callable $missingDepCallback
38 public function __construct( callable
$missingDepCallback ) {
39 $this->missingDepCallback
= $missingDepCallback;
46 public function checkDependencies() {
47 if ( !class_exists( Validator
::class ) ) {
48 call_user_func( $this->missingDepCallback
,
49 'The JsonSchema library cannot be found, please install it through composer.'
52 } elseif ( !class_exists( SpdxLicenses
::class ) ) {
53 call_user_func( $this->missingDepCallback
,
54 'The spdx-licenses library cannot be found, please install it through composer.'
63 * @param string $path file to validate
64 * @return bool true if passes validation
65 * @throws ExtensionJsonValidationError on any failure
67 public function validate( $path ) {
68 $data = json_decode( file_get_contents( $path ) );
69 if ( !is_object( $data ) ) {
70 throw new ExtensionJsonValidationError( "$path is not valid JSON" );
73 if ( !isset( $data->manifest_version
) ) {
74 throw new ExtensionJsonValidationError(
75 "$path does not have manifest_version set." );
78 $version = $data->manifest_version
;
79 $schemaPath = __DIR__
. "/../../docs/extension.schema.v$version.json";
82 if ( $version < ExtensionRegistry
::OLDEST_MANIFEST_VERSION
) {
83 throw new ExtensionJsonValidationError(
84 "$path is using a non-supported schema version"
86 } elseif ( $version > ExtensionRegistry
::MANIFEST_VERSION
) {
87 throw new ExtensionJsonValidationError(
88 "$path is using a non-supported schema version"
93 // Check if it's a string, if not, schema validation will display an error
94 if ( isset( $data->{'license-name'} ) && is_string( $data->{'license-name'} ) ) {
95 $licenses = new SpdxLicenses();
96 $valid = $licenses->validate( $data->{'license-name'} );
98 $extraErrors[] = '[license-name] Invalid SPDX license identifier, '
99 . 'see <https://spdx.org/licenses/>';
102 if ( isset( $data->url
) && is_string( $data->url
) ) {
103 $parsed = wfParseUrl( $data->url
);
105 if ( $parsed['host'] === 'www.mediawiki.org' ) {
107 } elseif ( $parsed['host'] === 'mediawiki.org' ) {
109 $extraErrors[] = '[url] Should use www.mediawiki.org domain';
112 if ( $mwoUrl && $parsed['scheme'] !== 'https' ) {
113 $extraErrors[] = '[url] Should use HTTPS for www.mediawiki.org URLs';
117 $validator = new Validator
;
118 $validator->check( $data, (object)[ '$ref' => 'file://' . $schemaPath ] );
119 if ( $validator->isValid() && !$extraErrors ) {
123 $out = "$path did not pass validation.\n";
124 foreach ( $validator->getErrors() as $error ) {
125 $out .= "[{$error['property']}] {$error['message']}\n";
127 if ( $extraErrors ) {
128 $out .= implode( "\n", $extraErrors ) . "\n";
130 throw new ExtensionJsonValidationError( $out );